package com.hyt.it.ogt.kq.main.controller.admission;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.gov.config.admission.AddOrUpdateAdmissionLockKeyCreator;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.admission.IAdmissionTicketInfoService;
import com.hyt.it.ogt.kq.service.gov.model.dto.admission.PageByCondition;
import com.hyt.it.ogt.kq.service.model.entity.admission.ExamineeInfo;
import com.hyt.it.ogt.kq.service.gov.model.param.admission.AddOrUpdateAdmissionTicketInfoParam;
import com.hyt.it.ogt.kq.service.gov.model.param.admission.ExamineeInfoExportParam;
import com.hyt.it.ogt.kq.service.gov.model.param.admission.UpdateAdmissionTicketInfoParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.admission.AdmissionDataSourceVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.admission.AdmissionKsNameVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.admission.AdmissionTicketInfoVO;
import com.hyt.it.ogt.kq.common.gov.progress.BatchGenerateAdmissionTicketLockKeyCreator;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.log.annotation.Log;
import com.hyt.loginfo.controller.BaseLoginController;
import com.hyt.loginfo.model.Organization;
import com.hyt.loginfo.model.User;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 准考证任务 前端控制器
 * </p>
 *
 * @author yanghuankun@talkweb.com.cn
 * @since 2023-1-13
 */
@RestController
@Api(tags = "234.准考证任务接口", value = "准考证任务接口")
@ApiSort(value = 234)
@RequestMapping("/kw/gov/admission/ticket/info")
@Slf4j
public class AdmissionTicketInfoController extends BaseLoginController {

    @Resource
    private IAdmissionTicketInfoService iAdmissionTicketInfoService;

    @ApiOperation(
            value = "34.1 创建或者编辑准考证任务",
            notes = "20000:成功; 43340101：创建或者编辑准考证任务失败"
    )
    @ApiOperationSort(value = 1)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "创建或者编辑准考证任务")
    @PostMapping
    @ProgressAnno(timeoutSeconds = 10 * 60, errorCode = 43340101, canConcurrent = false, lockKeyCreator = AddOrUpdateAdmissionLockKeyCreator.class)
    public void addOrUpdate(@RequestBody AddOrUpdateAdmissionTicketInfoParam addOrUpdateAdmissionTicketInfoParam) {
        log.info("[创建或者编辑准考证]任务 {}", addOrUpdateAdmissionTicketInfoParam);
        User user = getSysUser();
        iAdmissionTicketInfoService.addOrUpdate(addOrUpdateAdmissionTicketInfoParam, user, user.getOrganizationid());
    }

    @ApiOperation(
            value = "34.2 设置归档状态",
            notes = "20000:成功; 43340201：设置归档状态失败",
            response = ExamineeInfo.class
    )
    @ApiOperationSort(value = 2)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @ThrowsAdvice(code = 43340201, print = "更新准考证任务归档状态失败")
    @Log(module = "准考证任务管理", business = "设置归档状态")
    @PutMapping("/setArchiveStatus")
    public ApiResponse<Void> setArchiveStatus(@RequestBody UpdateAdmissionTicketInfoParam updateAdmissionTicketInfoParam) {
        User user = getSysUser();
        Organization deptInfo = getDeptInfo();
        iAdmissionTicketInfoService.setArchiveStatus(updateAdmissionTicketInfoParam, user, deptInfo);
        return ApiResponse.<Void>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .build();
    }

    @ApiOperation(
            value = "34.3 删除准考证任务",
            notes = "20000:成功; 43340301：删除准考证任务失败",
            response = ExamineeInfo.class
    )
    @ApiParam(name = "id", value = "准考证任务id")
    @ApiOperationSort(value = 3)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @ThrowsAdvice(code = 43340301, print = "删除准考证任务失败")
    @Log(module = "准考证任务管理", business = "删除准考证任务")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable String id) {
        User user = getSysUser();
        Organization deptInfo = getDeptInfo();
        iAdmissionTicketInfoService.delete(id, user, deptInfo);
        return ApiResponse.<Void>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .build();
    }

    @ApiOperation(
            value = "34.4 查询准考证任务列表",
            notes = "20000:成功; 43340401：查询准考证任务失败"
    )
    @ApiOperationSort(value = 4)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @ThrowsAdvice(code = 43340401, print = "查询准考证任务列表失败")
    @Log(module = "准考证任务管理", business = "查询准考证任务列表")
    @PostMapping("/pageByCondition")
    public ApiResponse<IPage<AdmissionTicketInfoVO>> pageByCondition(@RequestBody PageByCondition listByConditionDTO) {
        User user = getSysUser();
        Set<String> deptIds = getDeptIds();
        return ApiResponse.<IPage<AdmissionTicketInfoVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.pageByCondition(listByConditionDTO, user, deptIds))
                .build();
    }

    @ApiOperation(
            value = "34.5 导出考生名单",
            notes = "20000:成功;"
    )
    @ApiOperationSort(value = 5)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "导出考生名单")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 10, errorCode = 43340501)
    @GetMapping("/exportAdmissionTicketExcel")
    public void exportAdmissionTicketExcel(@Validated ExamineeInfoExportParam examineeInfoPageParam,
                                           BindingResult bindingResult) {
        this.checkValid(bindingResult);
        iAdmissionTicketInfoService.exportAdmissionTicketExcel(examineeInfoPageParam);
    }

    @ApiOperation(
            value = "34.6 批量下载准考证",
            notes = "20000:成功;",
            response = String.class
    )
    @ApiOperationSort(value = 6)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "批量下载准考证")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 60 * 5, errorCode = 43340601, canConcurrent = false, lockKeyCreator = BatchGenerateAdmissionTicketLockKeyCreator.class)
    @GetMapping("/batchDownloadAdmissionTicket")
    public void batchDownloadAdmissionTicket(@ApiParam(value = "准考证ID") @RequestParam String admissionTicketInfoId) {
        String userId = getUserId();
        iAdmissionTicketInfoService.batchDownloadAdmissionTicket(admissionTicketInfoId, userId);
    }

    @ApiOperation(
            value = "34.7 新增和编辑时查询数据来源",
            notes = "20000:成功; 43340701：查询数据来源失败"
    )
    @ApiOperationSort(value = 7)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "新增和编辑时查询数据来源")
    @GetMapping("/listDataSource")
    @ThrowsAdvice(code = 43340701, print = "查询数据来源失败")
    public ApiResponse<List<AdmissionDataSourceVO>> listDataSource(
            @ApiParam(name = "admissionTicketInfoId", value = "准考证任务的id，创建时不需要传递，编辑时需要传递")
            @RequestParam(required = false) String admissionTicketInfoId
    ) {
        return ApiResponse.<List<AdmissionDataSourceVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.listDataSource(admissionTicketInfoId))
                .build();
    }

    @ApiOperation(
            value = "34.8 新增和编辑时查询考试名称",
            notes = "20000:成功; 43340801：查询考试名称失败"
    )
    @ApiOperationSort(value = 8)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "新增和编辑时查询考试名称")
    @ThrowsAdvice(code = 43340801, print = "查询考试名称失败")
    @GetMapping("/listExam")
    public ApiResponse<List<AdmissionKsNameVO>> listExam(
            @ApiParam(name = "admissionTicketInfoId", value = "准考证任务的id，创建时不需要传递，编辑时需要传递")
            @RequestParam(required = false) String admissionTicketInfoId,
            @ApiParam(name = "dataSourceCode", value = "数据来源 1-无考场 2-虚拟考场")
            @RequestParam Integer dataSourceCode
    ) {
        User user = getSysUser();
        Set<String> deptIds = getDeptIds();
        return ApiResponse.<List<AdmissionKsNameVO>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.listExam(admissionTicketInfoId, dataSourceCode, user, deptIds))
                .build();
    }

    @ApiOperation(value = "34.9 发布状态（发布 / 取消发布）",
            notes = "20000:成功;",
            response = Boolean.class)
    @ApiOperationSort(value = 9)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "准考证发布状态操作")
    @ThrowsAdvice(code = 43340901, print = "准考证发布状态操作失败")
    @GetMapping("/setCertificateStatus")
    public ApiResponse<Boolean> setCertificateStatus(@ApiParam(value = "准考证ID") @RequestParam String admissionTicketInfoId,
                                                     @ApiParam(value = "发布状态 1-发布，0-取消发布") @RequestParam Boolean status) {
        return ApiResponse.<Boolean>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.setCertificateStatus(admissionTicketInfoId, status))
                .build();
    }

    @ApiOperation(value = "34.10 查询准考证任务详情",
            notes = "20000:成功;"
    )
    @ApiOperationSort(value = 10)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "查询准考证任务详情")
    @ThrowsAdvice(code = 43341001, print = "查询准考证是否已经生成数据失败")
    @GetMapping("/{id}")
    public ApiResponse<AdmissionTicketInfoVO> getByAdmissionId(@ApiParam(value = "准考证ID") @PathVariable("id") String id) {
        return ApiResponse.<AdmissionTicketInfoVO>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.getVOByAdmissionIdOrThrow(id))
                .build();
    }

    @ApiOperation(
            value = "34.11 批量生成准考证",
            notes = "20000:成功;",
            response = Boolean.class
    )
    @ApiOperationSort(value = 11)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "批量生成准考证")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 60 * 5, errorCode = 43341101, canConcurrent = false, lockKeyCreator = BatchGenerateAdmissionTicketLockKeyCreator.class)
    @GetMapping("/batchGenerateAdmissionTicket")
    public void batchGenerateAdmissionTicket(@ApiParam(value = "准考证ID") @RequestParam String admissionTicketInfoId) {
        String userId = getUserId();
        iAdmissionTicketInfoService.batchGenerateAdmissionTicket(admissionTicketInfoId, userId);
    }

    @ApiOperation(
            value = "34.12 通过虚拟考场id或者无考场id查询准考证详情",
            notes = "20000:成功; 43341201:失败"
    )
    @ApiOperationSort(value = 12)
    @ApiVersion(group = ApiVersionConstant.V2_4_6)
    @Log(module = "准考证任务管理", business = "通过虚拟考场id或者无考场id查询准考证详情")
    @ThrowsAdvice(code = 43341201, print = "通过虚拟考场id或者无考场id查询准考证详情失败")
    @GetMapping("/byExamId")
    public ApiResponse<AdmissionTicketInfoVO> getByExamId(@ApiParam(value = "虚拟考场id或者无考场id") @RequestParam String examId) {
        getSysUser();
        return ApiResponse.<AdmissionTicketInfoVO>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iAdmissionTicketInfoService.getVOByExamId(examId))
                .build();

    }
}
